<template>
  <el-dialog :title="'修改密码'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input type="password" v-model="dataForm.oldPassword" placeholder="旧密码"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword1">
        <el-input type="password" v-model="dataForm.newPassword1" placeholder="新密码"></el-input>
      </el-form-item>
      <el-form-item label="再次确认密码" prop="newPassword2">
        <el-input type="password" v-model="dataForm.newPassword2" placeholder="再次确认密码"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        storekeeperId: 0,
        oldPassword: "",
        newPassword1: "",
        // password: "",
        newPassword2: "",
      },
      dataRule: {
        oldPassword: [{ required: true, message: "不能为空", trigger: "blur" }],
        newPassword1: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        newPassword2: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.storekeeperId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
      });
    },
    // 表单提交
    dataFormSubmit() {
      if (this.dataForm.newPassword1 != this.dataForm.newPassword2) {
        alert("两次密码不一致");
      } else {
        this.$refs["dataForm"].validate((valid) => {
          if (valid) {
            this.$http
              .post(
                `${process.env.URL_HEAD}/store/storekeeper/updatePassword`,
                {
                  storeKeeperId: this.dataForm.storekeeperId || undefined,
                  oldPassword: this.dataForm.oldPassword,
                  newPassword: this.dataForm.newPassword1,
                },
                { withCredentials: true }
              )
              .then(({ data }) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: "操作成功",
                    type: "success",
                    duration: 1500,
                    onClose: () => {
                      this.visible = false;
                    //   this.$emit("refreshDataList");
                    },
                  });
                } else {
                  this.$message.error(data.msg);
                }
              });
          }
        });
        // alert("修改成功！");
      }
    },
  },
};
</script>
